<?php
$pageTitle = "培训统计表";
include 'header.php';
?>

<div class="row">
    <div class="col-md-12">
        <h2>培训统计查询</h2>
        <div class="row">
            <div class="col-md-3">
                    <label class="sr-only" for="school">单位</label>
                    <select id="school" class="form-control form-incline">
                        <option value="所有单位">所有单位</option>
                        <?php
                        $stmt = $pdo->query('SELECT school FROM users');
                        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                            echo '<option value="' . $row['school'] . '">' . $row['school'] . '</option>';
                        }
                        ?>
                    </select>
            </div>
            <div class="col-md-3">
                <label class="sr-only" for="start_time">开始时间</label>
                <input type="date" id="start_time" class="form-control">
            </div>
            <div class="col-md-3">
                <label class="sr-only" for="end_time">结束时间</label>
                <input type="date" id="end_time" class="form-control">
            </div>
            <div class="col-md-3">
                <button class="btn btn-primary" id="search">查询</button>
            </div>
        </div>
    </div>
    <div class="col-md-12"><br></div>
    <div class="col-md-12">
        <div class="table-responsive">
            <table class="table table-striped table-hover table-bordered">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>姓名</th>
                        <th>性别</th>
                        <th>单位</th>
                        <th>出生年月</th>
                        <th>参加工作时间</th>
                        <th>学科</th>
                        <th>培训主题</th>
                        <th>起止时间</th>
                        <th>地点</th>
                        <th>任务或自主</th>
                    </tr>
                </thead>
                <tbody>

                </tbody>
            </table>
        </div>
    </div>
</div>
<?php include 'footer.php'; ?>
<script src="https://cdn.staticfile.net/jquery/3.7.1/jquery.min.js"></script>
<script>
    $(document).ready(function () {
        $('#search').click(function () {
            var school = $('#school').val();
            var start_time = $('#start_time').val();
            var end_time = $('#end_time').val();

            $.ajax({
                url: 'report_training_api.php',
                type: 'POST',
                dataType: 'json',
                data: {
                    school: school,
                    start_time: start_time,
                    end_time: end_time
                },
                success: function (response) {
                    var tbody = $('tbody');
                    tbody.empty();

                    if (response.data.length === 0) {
                        tbody.append('<tr><td colspan="12">没有找到相关数据</td></tr>');
                        return;
                    }
                    var school = $('#school').val();
                    $.each(response.data, function (index, item) {
                        var row = '<tr>' +
                            '<td>' + (index + 1) + '</td>' +
                            '<td>' + item.teacher_name + '</td>' +
                            '<td>' + item.teacher_sex + '</td>' +
                            '<td>' + item.teacher_school + '</td>' +
                            '<td>' + item.teacher_birthday + '</td>' +
                            '<td>' + item.teacher_work_time + '</td>' +
                            '<td>' + item.teacher_subject + '</td>' +
                            '<td>' + item.title + '</td>' +
                            '<td>' + item.start_time + ' 至 ' + item.end_time + '</td>' +
                            '<td>' + item.location + '</td>' +
                            '<td>' + item.type + '</td>' +
                            '</tr>';

                        tbody.append(row);
                    });
                },
                error: function (xhr, status, error) {
                    console.error('搜索请求失败:', error);
                    alert('搜索请求失败，请检查控制台获取详细信息');
                }
            });
        });
    });
</script>